package com.peng.leetcode.array;

import java.util.HashMap;
import java.util.Map;

/**
 * MajorityElement
 * <p>
 * 169. 多数元素
 *
 * @author lupeng <lupeng@kuaishou.com>
 * Created on 2021-05-13
 */
public class MajorityElement {

    public static void main(String[] args) {

    }


    public int majorityElement(int[] nums) {
        int k = nums.length / 2;
        Map<Integer, Integer> countMap = new HashMap<>();

        for (int num : nums) {
            countMap.putIfAbsent(num, 0);
            countMap.put(num, countMap.get(num) + 1);
            if (countMap.get(num) > k) {
                return num;
            }
        }
        return -1;
    }



}
